package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.SysUpgradeDevLogAo;
import com.bjb.vr.common.dto.*;
import com.bjb.vr.common.mapper.ResDeviceMapper;
import com.bjb.vr.common.mapper.SysUpgradeDevLogMapper;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.utils.DateTimeUtil;
import com.bjb.vr.common.vo.DevUpgradeStatisticsVo;
import com.github.yitter.idgen.YitIdHelper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
* @author admin
* @description 针对表【sys_upgrade_dev_log(一体机升级记录表)】的数据库操作Service
* @createDate 2022-08-02 09:44:10
*/
@Slf4j
@Service
public class SysUpgradeDevLogService extends ServiceImpl<SysUpgradeDevLogMapper,SysUpgradeDevLog> {


    @Autowired
    SysUpgradeDevLogMapper sysUpgradeDevLogMapper;

    @Resource
    ResDeviceMapper resDeviceMapper;
    /**
     * 更新设备系统升级状态
     * @param udid
     * @param updateState
     * @param updateMsg
     * @return
     */
    public BaseResult updateSysUpdateState(String udid,int updateState,String updateMsg){
        sysUpgradeDevLogMapper.updateSysUpdateState(udid,updateState,updateMsg);
        return  CommonResult.Ok();
    }

    /**
     * 初始插入需要升级的设备记录
     * @param groupCode
     * @param tenantId
     * @param sysUpdateId
     * @return
     */
    public BaseResult initSysUpGradeDevLog(String groupCode,String tenantId,long sysUpdateId){
        MPJLambdaWrapper<SysUpgradeDevLog> mpjLambdaWrapper = new MPJLambdaWrapper<>();
            mpjLambdaWrapper
                    .select(ResDevice::getTenantId)
                    .select(ResDevice::getDevName)
                    .select(ResDevice::getUdid)
                    .selectAs(ResDevice::getUdid, "dev_udid")
                    .selectAs(ResDevice::getOnlineState,"dev_online_state")
                    .innerJoin(ResGroupRef.class,ResGroupRef::getDevUdid,ResDevice::getUdid)
                    .innerJoin(ResGroup.class,ResGroup::getId,ResGroupRef::getGroupId);
        mpjLambdaWrapper.eq(ResDevice::getTenantId,tenantId);
        mpjLambdaWrapper.eq(ResGroup::getGroupCode,groupCode);
        List<SysUpgradeDevLog>  sysUpgradeDevLogList =  resDeviceMapper.selectJoinList(SysUpgradeDevLog.class,mpjLambdaWrapper);
        buildNeedInfo(sysUpgradeDevLogList,sysUpdateId);
        this.saveBatch(sysUpgradeDevLogList);
        return  CommonResult.Ok();
    }

    public void buildNeedInfo(List<SysUpgradeDevLog>  sysUpgradeDevLogList,long sysUpdateId){
        LocalDateTime time = DateTimeUtil.getLocalDateTime(new Date());
        if(sysUpgradeDevLogList!=null && sysUpgradeDevLogList.size()>0){
            sysUpgradeDevLogList.forEach(sysUpgradeDevLog -> {
                sysUpgradeDevLog.setSysUpdateId(sysUpdateId);
                sysUpgradeDevLog.setUpgradeStatus(1);
                sysUpgradeDevLog.setUpdateDate(time);
                sysUpgradeDevLog.setGmtCreate(time);
                sysUpgradeDevLog.setDataId(YitIdHelper.nextId());
            });
        }
    }


    /**
     * 更新重新升级的设备记录为升级中
     * @param groupCode
     * @param tenantId
     * @param sysUpdateId
     * @return
     */
    public BaseResult reUpdSysUpGradeDevLog(String groupCode,String tenantId,long sysUpdateId,String udid){
        sysUpgradeDevLogMapper.reUpdSysUpGradeDevLog(groupCode,tenantId,sysUpdateId,udid);
        return  CommonResult.Ok();
    }
    /**
     * 查询升级设备统计信息
     * @param tenantId
     * @return
     */
    public DevUpgradeStatisticsVo queryDevSysUpgradeSumInfo(String tenantId){
        DevUpgradeStatisticsVo devUpgradeStatisticsVo = sysUpgradeDevLogMapper.queryDevSysUpgradeStatisticsInfo(tenantId);
        return devUpgradeStatisticsVo;
    }


    public IPage<SysUpgradeDevLog> queryDevSysUpgradeLogPage(String tenantId,SysUpgradeDevLogAo sysUpgradeDevLogAo){
        long current = (sysUpgradeDevLogAo.getCurrent() - 1) * sysUpgradeDevLogAo.getSize();
        List<SysUpgradeDevLog> sysUpgradeDevLogList = sysUpgradeDevLogMapper.queryDevSysUpgradeLogList(tenantId,current, sysUpgradeDevLogAo.getSize());
        long total = sysUpgradeDevLogMapper.queryDevSysUpgradeLogTotal(tenantId);
        IPage<SysUpgradeDevLog> voIPage = new Page<SysUpgradeDevLog>(sysUpgradeDevLogAo.getCurrent(), sysUpgradeDevLogAo.getSize(), total);
        voIPage.setRecords(sysUpgradeDevLogList);
        return voIPage;
    }

}
